/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pneuservis.frontend.web;

import java.util.List;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pneuservis.backend.services.CustomerServiceLocal;
import pneuservis.backend.to.CustomerTO;


/**
 *
 * @author jajaja
 */
@UrlBinding("/customers/{$event}/{customer.id}")
public class CustomersActionBean implements ActionBean{

    final static Logger log = LoggerFactory.getLogger(CustomersActionBean.class);
    
    private ActionBeanContext context;
    
    @ValidateNestedProperties(value = {
        @Validate(on = {"add", "save"}, field = "name", required = true),
        @Validate(on = {"add", "save"}, field = "phone", required = true),
        @Validate(on = {"add", "save"}, field = "address", required = true)
    })
    private CustomerTO customer;
    
    @EJB(lookup="java:global/cz.muni.fi.pa165.pneuservis_Pneuservis-ear_ear_1.0-SNAPSHOT/Pneuservis-ejb-1.0-SNAPSHOT/CustomerService")
    private CustomerServiceLocal customerService;

    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    public CustomerTO getCustomer() {
        return customer;
    }

    public void setCustomer(CustomerTO customer) {
        this.customer = customer;
    }
    
    public List<CustomerTO> getCustomers() {
        return customerService.getAllCustomers();
    }
    
    @DefaultHandler
    public Resolution all() {
        log.debug("all()");
        return new ForwardResolution("/showCustomer.jsp");
    }
    
    public Resolution add() {
        log.debug("add() customer={}", customer);
        System.out.println(customer);
        try {
        customerService.createCustomer(customer);
        } catch (EJBException ex) {
            log.warn(ex.getMessage());
        }
        return new RedirectResolution(this.getClass(), "all");
    }
    
    public Resolution delete() {
        log.debug("delete() customer={}", customer);
        try {
            customerService.removeCustomer(customer);
        } catch (EJBException e) {
            log.info(e.getMessage());
            //TO DO javascript error window
            return new RedirectResolution(this.getClass(), "all");
        }
        
        return new RedirectResolution(this.getClass(), "all");
    }
    
    public Resolution edit() {
        log.debug("edit() customer={}", customer);
        return new ForwardResolution("/editCustomer.jsp");
    }

    public Resolution save() {
        log.debug("save() customer={}", customer);
        customerService.updateCustomer(customer);
        return new RedirectResolution(this.getClass(), "all");
    }

        @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
    public void loadCustomerFromDatabase() {
        String ids = context.getRequest().getParameter("customer.id");
        if (ids == null) {
            return;
        }
        customer = customerService.getCustomer(Long.parseLong(ids));
    }
}